package org.tom.pet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tom.basic.exception.MyException;
import org.tom.basic.util.DistanceUtil;
import org.tom.basic.util.JsonResult;
import org.tom.basic.util.LoginContextUtil;
import org.tom.basic.util.PageList;
import org.tom.pet.domain.Pet;
import org.tom.pet.domain.SearchMasterMsg;
import org.tom.pet.query.SearchMasterMsgQuery;
import org.tom.pet.service.ISearchMasterMsgService;
import org.tom.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findAll();
    }

    /**
     * 寻主的接口
     * @param smm
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public JsonResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request); // 获取用户信息
            seachMasterMsgService.publish(smm,logininfo); // 调用service方法将寻主信息和登录用户的传过去
            return JsonResult.me();
        }catch (MyException e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("后端系统异常");
        }
    }

    /**
     * 待处理的寻主消息
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        // 获取用户登录用户信息
         Logininfo logininfo = LoginContextUtil.getLoginContext(request);

         // 将查询的数据，用户登录的id传到后端
         return seachMasterMsgService.pending(searchMasterMsgQuery,logininfo.getId());
     }


    /**
     * 已经处理的寻主消息
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        // 获取登录的用户信息
        Logininfo logininfo = LoginContextUtil.getLoginContext(request);
        // 将查询条件和用户信息的id传到service层
        return seachMasterMsgService.processed(searchMasterMsgQuery,logininfo.getId());
    }

    /**
     * 处理待处理的宠物信息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public JsonResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            // 获取登录人的信息
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            // 将宠物信息和登陆人的id传到service层
            seachMasterMsgService.handle(pet,logininfo.getId());
            return JsonResult.me();
        }catch (MyException e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("后端系统异常");
        }

    }

}
